<template>
  <view class="empty">
    <view class="box" v-if="status==1">
      <view class="box-p1">{{parkName}}</view>
      <view class="box-p2">入场时间：{{time}}</view>
      <view class="box-p3">欢迎光临</view>
    </view>
    <view class="box" v-if="status==2">
      <view class="box-p1">{{parkName}}</view>
      <view class="box-p2">入场时间：{{time}}</view>
      <view class="box-p3">车辆已入场，请扫描通道码离场</view>
    </view>
    <view class="box" v-if="status==3">
      <view class="box-p3">通道内无车辆</view>
    </view>
    <view class="box" v-if="status==4">
      <view class="box-p3">{{errorInfo}}</view>
    </view>
    <view class="btn" @tap="sureBtn">{{status==1?'确认':'返回'}}</view>
  </view>
</template>
<script>
import Taro from "@tarojs/taro";
import "./unlicensedIn.less"
import { setGlobalData, getGlobalData } from "../../globalData.js";
import bulterApi from "@souche2014/bulter-api";
const api = bulterApi({ partner: "IOParking" });
export default {
  data() {
    return {
      status:0,//无牌车入场状态：1：成功  2：已入场  3：此通道没有无牌车
      channelCode:'',
      parkingCode:'',
      carInfos:null,
      mobile:'',
      codeType: "3", //1:场内，2：通道，3：无牌车
      errorInfo:'',
      time:'',
      parkName:'',
      apiBaseUrl: process.env.IOParking.API_HOST //请求头
    };
  },
  onLoad: function(options) {
    console.log("页面参数", options);
    var that = this;
    if (options.channelCode) {
      //体验开发版本扫码获得参数
      that.channelCode = options.channelCode ? options.channelCode : "";
    } else if (options.q) {
      //正式版扫码获得参数
      let qrUrl = decodeURIComponent(options.q);
      that.channelCode = that.getQueryString(qrUrl, "channelCode");
    }
      if (getGlobalData("openId")) {
        that.unliceData();
      } else {
        that.wx_login();
      }
    
  },
  created(){
    
  },
  methods:{
    wx_login() {
      api
        .getUserInfo({
          scope: "all"
        })
        .then(resp => {
          if (resp.success) {
            console.log("authCode is：", resp.data);
            if (resp.data == "mock") {
              setGlobalData("openId", "oRPxj5ZrsZGRuxTa-UrzEBeLuv-s");
              //setGlobalData("mobile", "17761643218");
              this.unliceData();
              return;
            }
            this.getInfos(resp.data);
          } else {
            console.log("err is ", resp.message, "err code is ", resp.code);
          }
        });
    },
    getInfos(authCode) {
      let that = this;
      Taro.request({
        url: this.apiBaseUrl + "/weixin/souchegetuser?authCode=" + authCode,
        data: {},
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "GET",
        success(res) {
          console.log(res.data);
          if (res.data.isSuccess) {
            var userOpenId = res.data.data.openId;
            setGlobalData("openId", userOpenId);
            setGlobalData("mobile", res.data.data.phoneNum);
            that.unliceData();
            api.bury("userAuthorizationNum", {
              time: new Date().valueOf(),
              loginState: false
            });
          }
        }
      });
    },
    unliceData(){
      Taro.showLoading({
        title: "加载中",
        mask: true
      });
      var that=this
      Taro.request({
        url: that.apiBaseUrl + "/unlicensedcar/unlicensedcarsin",
        data: {
          wxOpenId: getGlobalData("openId"),
          mobile: getGlobalData("mobile"),
          channelCode: that.channelCode
        },
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "POST",
        success(res) {
          Taro.hideLoading();
          console.log("无牌车进入", res.data);
          if (res.data.isSuccess) {
           if(res.data.data.code=='200'){//正常
             that.status=1
             that.parkName=res.data.data.parkName
             that.time=res.data.data.time
           }else if(res.data.data.code=='500'){//其他类型的异常
             that.status=4
             that.errorInfo=res.data.data.msg
           }else if(res.data.data.code=='5001'){//车辆已入场,请扫描通道码离场
             that.status=2
             that.parkName=res.data.data.parkName
             that.time=res.data.data.time
           }
           else if(res.data.data.code=='5002'){//通道内无车辆
             that.status=3
           }
          }else{
            that.status=4
            that.errorInfo=res.data.message
          }
        }
      });
    },
    sureBtn(){
      if(this.status==1){
        api.openPage("home");
      }else{
        api.openPage("home");
      }
    },
    //解析链接方法
    getQueryString(url, name) {
      var reg = new RegExp("(^|&|/?)" + name + "=([^&|/?]*)(&|/?|$)", "i");
      var r = url.substr(1).match(reg);
      if (r != null) {
        return r[2];
      }
      return null;
    }
  }
};
</script>
